package com.gbr.view.recordingarea;

import java.awt.Rectangle;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

import javax.swing.JDialog;

public class RecordingAnnotation {
    private JDialog dialog;

    public RecordingAnnotation() {
        initComponent();
    }

    private void initComponent() {
        dialog = new JDialog();
        dialog.setUndecorated(true);
        dialog.setBackground(new java.awt.Color(0, 0, 0, 0));
        dialog.getContentPane().setBackground(new java.awt.Color(0, 0, 0, 0));
        dialog.setAlwaysOnTop(true);
    }

    private void drawRectangle(Canvas canvas, Rectangle rect) {
        final GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.clearRect(0, 0, rect.getWidth(), rect.getHeight());
        gc.setStroke(Color.RED);
        gc.setLineWidth(5);
        gc.strokeRect(0, 0, rect.getWidth(), rect.getHeight());
    }

    public void open(Rectangle r) {
        Rectangle rect = new Rectangle(r.x - 5, r.y - 5, r.width + 10, r.height + 10);

        Canvas canvas = new Canvas(rect.getWidth(), rect.getHeight());
        Group root = new Group();
        root.getChildren().add(canvas);
        final Scene scene = new Scene(root, rect.getWidth(), rect.getHeight(), Color.rgb(0, 0, 0, 0));
        final JFXPanel fxPanel = new JFXPanel();
        fxPanel.setOpaque(false);
        fxPanel.setBackground(java.awt.Color.BLUE);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                fxPanel.setScene(scene);
            }
        });
        dialog.add(fxPanel);

        drawRectangle(canvas, rect);
        dialog.setBounds(rect);
        dialog.setVisible(true);
    }

    public void close() {
        dialog.setVisible(false);
        dialog.getContentPane().removeAll();
    }
}
